package crmdna.custom.isha;

import java.util.List;
import java.util.Set;

import crmdna.client.member.Member;
import crmdna.client.member.Member.CheckInMemberProp;
import crmdna.client.program.Program;
import crmdna.client.program.Program.ProgramProp;
import crmdna.common.APIException;
import crmdna.common.APIResponse.Status;
import crmdna.crmdna.Client;

public class IshaMember {
	private static final String ISHA = "isha";
		
	static boolean isMeditator(Set<Long> practiceIds) {
		Set<Long> sathsangPracticeIds = IshaConfig.safeGet().sathsangPracticeIds;
						
		for (Long practiceId : sathsangPracticeIds) {
			if (practiceIds.contains(practiceId))
				return true;
		}
		
		return false;
	}
	
	static List<CheckInMemberProp> getMembersForCheckIn(String searchStr, long programId, 
			int sessionDateYYYYMMDD,
			int maxResultSize, String login) {
				
		Client.ensureValidClient(ISHA);
		ProgramProp programProp = Program.safeGet("isha", programId).toProp();
		
		if (! IshaProgram.isSathsang(programProp.name)) {			
			throw new APIException(Status.ERROR_NOT_IMPLEMENTED, 
					"Check in not available yet for [" 
					+ programProp.programTypeProp.displayName + "]");
		}
		
		List<CheckInMemberProp> checkInMemberProps = 
				Member.getMembersForCheckIn(ISHA, searchStr, programId, sessionDateYYYYMMDD, login);
		
		
		for (CheckInMemberProp checkInMemberProp : checkInMemberProps) {
			if (! isMeditator(checkInMemberProp.practiceIds)) {
				checkInMemberProp.allow = false;
				checkInMemberProp.notAllowingReason = "Not meditator";
			}
		}
		
		if ((maxResultSize != 0) && (maxResultSize > checkInMemberProps.size()))
				checkInMemberProps = checkInMemberProps.subList(0, maxResultSize);
				
		return checkInMemberProps;											
	}							
}
